package util;

import java.text.DecimalFormat;

public class SizeUtil {
    private final static long KB = (long) Math.pow(1024, 1);
    private final static long MB = (long) Math.pow(1024, 2);
    private final static long GB = (long) Math.pow(1024, 3);
    private final static long TB = (long) Math.pow(1024, 4);
    private final static long PB = (long) Math.pow(1024, 5);
    private final static int B = 1024;
    
    public static String round(double num) {
        DecimalFormat df = new DecimalFormat("##.##");
        return df.format(num);
    }

    public static String getSize(long num) {
        int n = (int) (Math.log(num) / Math.log(B));
        double numD = (double) num;
        switch (n) {
            case 0:
                return round(numD) + "B";
            case 1:
                return round(numD / KB) + "KB";
            case 2:
                return round(numD / MB) + "MB"; 
            case 3:
                return round(numD / GB) + "GB";
            case 4:
                return round(numD / TB) + "TB";
            case 5:
                return round(numD / PB) + "PB";
            default:
            	return String.valueOf(0) + "B";
        }
    }
}
